/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-06-28 08:09:51
 * @LastEditTime: 2022-07-17 23:20:50
 * @LastEditors: liutq
 * @Reference:
 */
package goods

import (
	"errors"
	"time"

	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/model"
	"github.com/maxyang107/jinglan/model/user/repo"
)

type Repo struct {
	ID                    uint      `gorm:"column:id; parimary_key" json:"id"`
	UserId                uint      `gorm:"column:uid; " json:"uid"`
	CollectId             uint      `gorm:"column:collect_id; " json:"collect_id"`
	GoodsId               uint      `gorm:"column:goods_id;" json:"goods_id"`
	OpenAccountName       string    `gorm:"column:open_account_name;" json:"open_account_name"`
	BankAccount           string    `gorm:"column:bank_account;" json:"bank_account"`
	OpenAccountBank       string    `gorm:"column:open_account_bank;" json:"open_account_bank"`
	AccountOpeningOutlets string    `gorm:"column:account_opening_outlets;" json:"account_opening_outlets"`
	ShaiUrl               string    `gorm:"column:shai_url;" json:"shai_url"`
	Status                uint8     `gorm:"column:status;" json:"status"`
	Createtime            time.Time `gorm:"column:createtime;" json:"createtime"`
}

type RepoInfo struct {
	ID                    uint   `gorm:"column:id; parimary_key" json:"id"`
	UserId                uint   `gorm:"column:uid; " json:"uid"`
	CollectId             uint   `gorm:"column:collect_id; " json:"collect_id"`
	GoodsId               uint   `gorm:"column:goods_id;" json:"goods_id"`
	GoodsName             string `gorm:"column:goods_name" json:"goods_name"`
	OpenAccountName       string `gorm:"column:open_account_name;" json:"open_account_name"`
	BankAccount           string `gorm:"column:bank_account;" json:"bank_account"`
	OpenAccountBank       string `gorm:"column:open_account_bank;" json:"open_account_bank"`
	AccountOpeningOutlets string `gorm:"column:account_opening_outlets;" json:"account_opening_outlets"`
	ShaiUrl               string `gorm:"column:shai_url;" json:"shai_url"`
	Status                uint8  `gorm:"column:status;" json:"status"`
}

type RepoList struct {
	List    []RepoInfo
	Page    uint
	PerPage uint
	Total   uint
}

func AddRepoRecord(param common.Repo) error {
	var tmp Repo

	model.Db.Where(&Repo{CollectId: param.CollectId, UserId: param.UId}).First(&tmp)

	if tmp.ID > 0 {
		return errors.New("已经提交过回购申请了，请勿重复提交")
	}

	mcd := repo.GetUserCollectDetailById(param.UId, param.CollectId)
	if mcd.ID == 0 || mcd.GoodsType != 1 {
		return errors.New("不支持的请求")
	}

	t1, err1 := time.Parse("2006-01-02 15:04:05", time.Now().Format("2006-01-02 15:04:05"))
	t2, err2 := time.Parse("2006-01-02 15:04:05", mcd.RepoStartTime.Format("2006-01-02 15:04:05"))
	if err1 == nil && err2 == nil && t1.After(t2) {
		return errors.New("不支持的请求")
	}
	err := model.Db.Create(&param).Error
	return err
}

func GetRepoList(param common.RepoList) *RepoList {
	var resp RepoList

	resp.Page = param.Page
	resp.PerPage = param.PerPage
	resp.Total = GetRepoTotal(param)
	tx := model.Db.Model(&Repo{})
	if param.CollectId > 0 {
		tx = tx.Where(&Repo{CollectId: param.CollectId})
	}

	tx.Limit(param.PerPage).Offset((param.Page - 1) * param.PerPage).Scan(&resp.List)

	return &resp
}

func GetRepoTotal(param common.RepoList) uint {
	var total uint
	if param.CollectId == 0 {
		model.Db.Model(&Repo{}).Count(&total)
	} else {
		model.Db.Where(&Repo{CollectId: param.CollectId}).Count(&total)
	}
	// pageNum := total / param.PerPage
	// if total%param.PerPage != 0 {
	// 	pageNum++
	// }
	return total
}

func ModefyRepoStatus(param common.RepoModify) error {
	var tmp Repo

	model.Db.Where(&Repo{ID: param.Id}).First(&tmp)

	if tmp.ID == 0 {
		return errors.New("没有找到该信息")
	}

	err := model.Db.Model(&Repo{}).Update(&param).Error
	if err != nil {
		return err
	}

	var mycollect_ repo.MyCollect
	mycollect_.ID = tmp.CollectId

	if param.Status == 1 {
		mycollect_.RepoStatus = 4
		mycollect_.HistoryOwner = tmp.UserId
		mycollect_.OwnerUid = common.RepoUid
	}

	if param.Status == 2 {
		mycollect_.RepoStatus = 3
	}
	err2 := repo.UpdateMyCollect(&mycollect_)
	if err2 != nil {
		return err
	}
	return nil
}
